
#import "UIAlertViewAdditions.h"

@implementation UIAlertView(Additions)

//
// dismiss callback
//
static UIAlertViewDismissCallback globalDismissCallback						= nil;

//
// cancel callback
//
static UIAlertViewCancelCallback globalCancelCallback						= nil;

//
// with title, message, cancel, other
//
+ (UIAlertView*)alertViewWithTitle:(NSString*)title message:(NSString*)message cancel:(NSString*)cancel
{
	return [self alertViewWithTitle:title message:message cancel:cancel others:nil onDismiss:nil onCancel:nil];
}

//
// with title, message, cancel, other
//
+ (UIAlertView*)alertViewWithTitle:(NSString*)title message:(NSString*)message cancel:(NSString*)cancel others:(NSArray*)others onDismiss:(UIAlertViewDismissCallback)onDismiss onCancel:(UIAlertViewCancelCallback)onCancel
{
	[globalCancelCallback release];
	globalCancelCallback													= [onCancel copy];
	
	[globalDismissCallback release];
	globalDismissCallback													= [onDismiss copy];
	
	UIAlertView* alert														= [[UIAlertView alloc] initWithTitle:title message:message delegate:[self class] cancelButtonTitle:cancel otherButtonTitles:nil];
	for(NSString* other in others)
		[alert addButtonWithTitle:other];
	
	[alert show];
	return [alert autorelease];
}

//
// delegate
//
+ (void)alertView:(UIAlertView*)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
	if(buttonIndex == [alertView cancelButtonIndex])
	{
		if(globalCancelCallback)
			globalCancelCallback();
	}
	else
	{
		if(globalDismissCallback)
			globalDismissCallback(buttonIndex - 1);
	}
}
@end
